package com.books.activity;

import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.utils.TextUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.books.R;

import com.books.adapter.LRYBooksSearchAdapter;
import com.books.databinding.BooksSearchActivityBinding;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

import anime.room.HistoryViewModel;
import base.QActivity;
import base.QLoading;
import books.room.BooksHistoryViewModel;
import http.Api;
import model.BookLRYSearch;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class BooksSearchActivity extends QActivity {

    private BooksHistoryViewModel viewModel;
    private String searchName;
    private BooksSearchActivityBinding binding;
    private LRYBooksSearchAdapter bookadapter;
    private QLoading qLoading;

    @Override
    public int getLayoutRes() {
        return R.layout.books_search_activity;
    }

    @Override
    public void initView() {
        binding = DataBindingUtil.setContentView(this, getLayoutRes());
        viewModel = new ViewModelProvider(this).get(BooksHistoryViewModel.class);

    }

    @Override
    public void initData() {

        Bundle bundle = getIntent().getExtras();
        searchName = bundle.getString("searchName");
        binding.edSearch.setText(searchName);


        LinearLayoutManager yhlayoutManager = new LinearLayoutManager(this);
        yhlayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.rvLrysearch.setLayoutManager(yhlayoutManager);
        bookadapter = new LRYBooksSearchAdapter(R.layout.books_search_item, null);
        binding.rvLrysearch.setAdapter(bookadapter);


    }

    @Override
    public void initListener() {
        binding.edSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                //以下方法防止两次发送请求
                if (actionId == EditorInfo.IME_ACTION_SEND ||
                        (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    switch (event.getAction()) {
                        case KeyEvent.ACTION_UP:
                            searchName = binding.edSearch.getText().toString().trim();
                            networkMessage();
                            return true;
                        default:
                            return true;
                    }
                }
                return false;
            }

        });

        binding.edSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                searchName = binding.edSearch.getText().toString().trim();
                networkMessage();

            }
        });

        binding.ivSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchName = binding.edSearch.getText().toString().trim();
            }
        });
    }

    @Override
    public void networkMessage() {
        qLoading = new QLoading();
        qLoading.showNow(getSupportFragmentManager(), "");
        if (TextUtils.isEmpty(searchName)) {
            ToastUtils.showLong("搜索内容为空，请输入正确内容");
            return;
        }
        Call<BookLRYSearch> call = Api.getInstance().service.getLRYBookSearch("title", searchName, 1, 30);
        call.enqueue(new Callback<BookLRYSearch>() {
            @Override
            public void onResponse(Call<BookLRYSearch> call, Response<BookLRYSearch> response) {
                LogUtils.d("网络请求数据" + call.toString(), response.body());

                if (response.body() != null && response.body().getData() != null) {

                    bookadapter.replaceData(response.body().getData());


                } else {

                    if(response.body()!=null && response.body().getMsg()!=null){
                        ToastUtils.showLong(response.body().getMsg() + "状态码:" + response.code());
                    }
                }

                if (qLoading != null) {
                    qLoading.dismiss();
                }

            }

            @Override
            public void onFailure(Call<BookLRYSearch> call, Throwable t) {
                LogUtils.d("请切换模式试下  网络请求失败:" + t.getMessage());
                ToastUtils.showLong("请切换模式  网络请求失败:" + t.getMessage());
                if (qLoading != null) {
                    qLoading.dismiss();
                }
            }
        });
    }
}
